package com.hero.jobweb.service.impl;

import static com.hero.jobweb.config.MessagesManger.getSystemMessages;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hero.basicsystem.dao.IDepartmentDAO;
import com.hero.basicsystem.dao.ext.ICodesortDAOExt;
import com.hero.basicsystem.entity.Codesort;
import com.hero.basicsystem.entity.Department;
import com.hero.jobweb.config.ExceptionKey;
import com.hero.jobweb.config.SystemKey;
import com.hero.jobweb.dao.ITbUsersDAO;
import com.hero.jobweb.dao.ext.ITbOperateLogDAOExt;
import com.hero.jobweb.entity.TbOperateLog;
import com.hero.jobweb.entity.TbOperateLogExample;
import com.hero.jobweb.entity.TbUsers;
import com.hero.jobweb.entity.TbUsersExample;
import com.hero.jobweb.exception.BaseException;
import com.hero.jobweb.service.ICommonService;

public class CommonServiceImpl extends BaseService implements ICommonService {
	private ITbOperateLogDAOExt tbOperateLogDAO;
	private ITbUsersDAO tbUsersDAO;
	private ICodesortDAOExt codeSortDAO;
	private IDepartmentDAO departmentDAO;

	public IDepartmentDAO getDepartmentDAO() {
		return departmentDAO;
	}

	public void setDepartmentDAO(IDepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public ICodesortDAOExt getCodeSortDAO() {
		return codeSortDAO;
	}

	public void setCodeSortDAO(ICodesortDAOExt codeSortDAO) {
		this.codeSortDAO = codeSortDAO;
	}

	public ITbUsersDAO getTbUsersDAO() {
		return tbUsersDAO;
	}

	public void setTbUsersDAO(ITbUsersDAO tbUsersDAO) {
		this.tbUsersDAO = tbUsersDAO;
	}

	public ITbOperateLogDAOExt getTbOperateLogDAO() {
		return tbOperateLogDAO;
	}

	public void setTbOperateLogDAO(ITbOperateLogDAOExt tbOperateLogDAO) {
		this.tbOperateLogDAO = tbOperateLogDAO;
	}

	public void saveOperateLog(TbOperateLog operateLog) {
		this.tbOperateLogDAO.insert(operateLog);
	}

	public void removeOperateLog(TbOperateLog operateLog) {
		TbOperateLogExample operateLogExample = new TbOperateLogExample();
		operateLogExample.createCriteria().andIdEqualTo(operateLog.getId());
		this.tbOperateLogDAO.deleteByExample(operateLogExample);
	}

	public List<TbOperateLog> queryOperateLog(TbOperateLog operateLog) {
		TbOperateLogExample operateLogExample = new TbOperateLogExample();
		operateLogExample.setPagination(operateLog.getPagination());
		return this.tbOperateLogDAO.selectByExamplePage(operateLogExample);
	}

	/**
	 * 用户登录验证
	 */
	public TbUsers userLogin(TbUsers user) {
		TbUsersExample usersExample = new TbUsersExample();
		TbUsersExample.Criteria cri = usersExample.createCriteria();
		cri.andNAMEEqualTo(user.getNAME());
		cri.andPASSWORDEqualTo(user.getPASSWORD());
		List<TbUsers> returnUsers = this.tbUsersDAO
				.selectByExample(usersExample);
		if (returnUsers == null || returnUsers.size() != 1) {
			throw new BaseException(ExceptionKey.USER_PASSWORD_ERROR);
		}
		TbUsers loginUser = returnUsers.get(0);

		String userStatus = loginUser.getSTATUS();// 用户状态
		if(StringUtils.equals(userStatus,getSystemMessages(SystemKey.ACCOUT_LOCKED_STATUS)))// 账户被锁定
		{
			throw new BaseException(ExceptionKey.ACCOUT_LOCKED);
		}
		if (StringUtils.equals(loginUser.getUserTypeCode(),getSystemMessages(SystemKey.DEFAULT_ENTERPRISE_USER_TYPE_CODE)))// 企业用户
		{
			// 查询部门
			Department department = queryDepartmentById(loginUser
					.getDepartmentId());
			if (department == null) {
				throw new BaseException(ExceptionKey.ACCOUNT_UNKNOW_DEPARTMENT);// 未归属部门
			}
			loginUser.setDepartment(department);
		}
		
		return loginUser;
	}

	/**
	 * 分布式事务处理测试 并且测试成功
	 */
	public void jtaTest() {

		// BasicSystem库
		Codesort codeSort = new Codesort();
		codeSort.setName("事物测试");
		codeSort.setCode("0111110");
		codeSort.setUpCode("");
		codeSort.setSerial(0);
		codeSortDAO.jtaInsert(codeSort);
		// JobWeb库
		TbOperateLog operateLog = new TbOperateLog();
		operateLog.setTypeCode("00035");
		operateLog.setIP("192.168.0.1");
		operateLog.setRemark("fffffffffffffffffffffffffffffffffadsafsaf");
		operateLog.setDescription("fffffffffffffffffffffffffffffffffadsafsaf");
		operateLog.setCreateTime(Calendar.getInstance().getTime());
		operateLog.setUserId(1);
		this.tbOperateLogDAO.jtaInsert(operateLog);
	}

	/**
	 * 用部门标识查询部门
	 */
	public Department queryDepartmentById(Integer dId) {
		return this.departmentDAO.selectByPrimaryKey(dId);
	}

}
